package org.usmile.algorithms.leetcode.simple;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * 剑指 Offer 09. 用两个栈实现队列
 * <p>
 * 用两个栈实现一个队列。队列的声明如下，请实现它的两个函数 appendTail 和 deleteHead ，分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素，deleteHead 操作返回 -1 )
 * <p>
 * 示例 1：
 * <p>
 * 输入：
 * ["CQueue","appendTail","deleteHead","deleteHead"]
 * [[],[3],[],[]]
 * 输出：[null,null,3,-1]
 * <p>
 * 示例 2：
 * <p>
 * 输入：
 * ["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
 * [[],[],[5],[2],[],[]]
 * 输出：[null,-1,null,null,5,2]
 * <p>
 * 提示：
 * <p>
 * 1 <= values <= 10000
 * 最多会对 appendTail、deleteHead 进行 10000 次调用
 */
public class Offer_009 {
}


class Offer_009_Solution {
    private static class CQueue {
        Deque<Integer> head = new ArrayDeque<>();
        Deque<Integer> tail = new ArrayDeque<>();

        public CQueue() {

        }

        public void appendTail(int value) {
            tail.push(value);
        }

        public int deleteHead() {
            if (head.isEmpty()) {
                if (tail.isEmpty()) {
                    return  -1;
                }
                while (!tail.isEmpty()) {
                    head.push(tail.pop());
                }
            }

            return head.pop();
        }
    }
}